/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package issr.boundary;

import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.DateField;
import com.vaadin.ui.Form;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.InlineDateField;
import com.vaadin.ui.Select;
import com.vaadin.ui.TextField;
import issr.boundary.vaadin.AbstractView;
import issr.boundary.vaadin.MainApplication;
import issr.controller.ControlloreInserimentoLotto;
import issr.controller.ControlloreInserimentoLotto_Service;
import issr.controller.Prodotto;
import issr.controller.Utente;
import issr.misc.Labels;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Soulreaver
 */
public class PannelloInserimentoLotto extends AbstractView implements Button.ClickListener {

    private ControlloreInserimentoLotto_Service cls;
    private ControlloreInserimentoLotto controllore;
    private final Select prodottoSelect = new Select(Labels.PRODOTTO);
    private final TextField gs1BarCodeField = new TextField(Labels.CODICE_A_BARRE_GS1);
    private final TextField quantitaField = new TextField(Labels.QUANTITA);
    private final DateField dateField = new InlineDateField(Labels.DATA_SCADENZA);
    private final Button submit;
    private final Form form = new Form();
    private final FormLayout layout;

    public PannelloInserimentoLotto() {
        cls = new ControlloreInserimentoLotto_Service();
        controllore = cls.getControlloreInserimentoLottoPort();
        layout = new FormLayout();
        // Create the visual components of the view.
        setSizeFull();
        setCompositionRoot(layout);
        layout.setHeight("100%");
        layout.setWidth("100%");
        layout.setSpacing(true);
        layout.setMargin(true);

        gs1BarCodeField.setRequired(true);
        quantitaField.setRequired(true);
        dateField.setRequired(true);
        dateField.setDateFormat("dd-MM-yyyy");
        dateField.setResolution(DateField.RESOLUTION_DAY);
        prodottoSelect.setRequired(true);
        form.addField(Labels.PRODOTTO, prodottoSelect);
        form.addField(Labels.CODICE_A_BARRE_GS1, gs1BarCodeField);
        form.addField(Labels.QUANTITA, quantitaField);
        form.addField(Labels.DATA_SCADENZA, dateField);
        layout.addComponent(form);

        submit = new Button("Submit", this);
        submit.setClickShortcut(KeyCode.ENTER);
        layout.addComponent(submit);
        setCompositionRoot(layout);
    }

    @Override
    protected void close() throws Exception {
    }

    @Override
    protected void open() throws Exception {
        visualizzaForm();
    }

    protected void resetForm() {
        prodottoSelect.setValue("");
        prodottoSelect.removeAllItems();
        gs1BarCodeField.setValue("");
        quantitaField.setValue("");
        dateField.setValue(null);
    }

    protected void visualizzaForm() {
        resetForm();
        List<Prodotto> catalogo = controllore.ottieniCatalogo(((Utente) MainApplication.getProject().getUser()).getPuntovendita());
        for (Prodotto prod : catalogo) {
            prodottoSelect.addItem(prod);
        }
    }

    @Override
    public void buttonClick(ClickEvent event) {
        int gs1BarCode = Integer.parseInt((String) gs1BarCodeField.getValue());
        int quantita = Integer.parseInt((String) quantitaField.getValue());
        DateFormat formatter;
        formatter = new SimpleDateFormat("dd-MM-yyyy");
        String scadenzaAsString = formatter.format((Date) dateField.getValue());

        if (controllore.inserisciLotto(
                (Prodotto) prodottoSelect.getValue(),
                gs1BarCode,
                quantita,
//                (Date) dateField.getValue(),
                scadenzaAsString,
                (Utente) MainApplication.getProject().getUser()) == true) {
            MainApplication.getProject().getMainWindow().showNotification(
                    "Lotto " + gs1BarCode + " inserito con successo");

            resetForm();
        } else {
            MainApplication.getProject().getMainWindow().showNotification(
                    "Errore inserimento lotto");
        }
    }
}
